const skeletonSettings = { zbp: { name: '博客标题', subname: '副标题', host: '/' } };
const skeletonPost = {
  ID: 0,
  Title: '文章加载中',
  Intro: '',
  Content: '<div>文章加载中</div>',
  Category: { ID: 0, Name: '未命名' },
  Author: { ID: 0, Name: '作者' },
  PostTime: 1626692375,
  AllImages: [],
  loading: 1,
};
import api from '@/base/api.js';

import tools from '@/base/utils/tools.js';

export const objStore = {
  state() {
    return {
      cache: { cate: {} },
      host: window.cookiespath ? window.cookiespath : '/',
      copyright: window.copyright ? window.copyright : '',
      settings: skeletonSettings,
      posts: [
        skeletonPost,
        skeletonPost,
        skeletonPost,
        skeletonPost,
        skeletonPost,
        skeletonPost,
      ],
      post: {},
      postsPagination: {},
      navbar: {},
    };
  },
  mutations: {
    // 占位内容填充
    setSkeletonPosts(state) {
      state.posts.forEach((element, i) => {
        state.posts[i] = skeletonPost;
      });
    },
    // setSkeletonSettings(state) {
    //   state.settings = skeletonSettings
    // },
    setSkeletonPost(state) {
      state.post = skeletonPost;
    },
    // 数据写入
    saveSettings(state, settings) {
      state.settings = settings;
      // state.host = settings.zbp.host
      tools.setTitle('System', settings.zbp);
    },
    savePosts(state, posts) {
      state.posts = posts.list;
      state.postsPagination = posts.pagebar;
      state.postsPagination.PagebarCount = state.settings.zbp.pagebar_count;
      tools.setTitle('Index', { msg: 'savePosts' });
    },
    savePost(state, post) {
      state.post = post;
      tools.setTitle('Post', post);
    },
    saveNavbar(state, navbars) {
      navbars.Content = tools.htmlEscape(navbars.Content);
      state.navbar = navbars;
    },
    //
    saveCacheCate(state, params) {
      if (!Object.hasOwnProperty.call(state.cache.cate, params.name)) {
        state.cache.cate[params.name] = params.id;
      }
    },
    saveCacheQuery(state, query) {
      state.cache.query = query;
    },
  },
  actions: {
    getSettings(context) {
      tools.setTitle('Load', { msg: 'getSettings' });
      api({
        query: {
          mod: 'system',
          act: 'basic_info',
        },
      })
        .then((res) => {
          context.commit('saveSettings', res);
        })
        .catch((err) => {
          console.log(err);
        });
    },
    getPosts(context, params = {}) {
      tools.setTitle('Load', { msg: 'getPosts', params });
      // 触发占位
      context.commit('setSkeletonPosts');
      const defaultParams = {
        mod: 'post',
        act: 'list',
        type: '0',
        sortby: 'PostTime',
        order: 'DESC',
        with_relations: 'Author,Category',
        page: 1,
      };
      params = Object.assign(defaultParams, params);

      api({
        query: params,
      }).then((res) => {
        context.commit('savePosts', res);
      });
    },
    getPost(context, params) {
      tools.setTitle('Load', { msg: 'getPost' });
      // 触发占位
      context.commit('setSkeletonPost');
      api({
        query: {
          mod: 'post',
          id: params.id,
          with_relations: 'Author,Category',
        },
      })
        .then((res) => {
          context.commit('savePost', res.post);
        })
        .catch((err) => {
          console.log(err);
        });
    },
    getNavbar(context) {
      api({
        query: {
          mod: 'module',
          filename: 'navbar',
        },
      })
        .then((res) => {
          context.commit('saveNavbar', res.module);
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
};
